import { createRouter, createWebHashHistory } from 'vue-router'
import { usestore } from '../store'
// 导入进度条
import { start, close } from "../utils/nporgress";

//路由懒加载
const login = () => import('@/Views/LoginPage.vue')
const home = () => import('@/Views/HomePage.vue')
const register = () => import('@/Views/RegisterPage.vue')
const NotFound = () => import('@/Views/NotFound.vue')
const newindex = () => import('@/Views/homenav/NewIndex.vue')
const newcolumn = () => import('@/Views/homenav/NewsColumn.vue')
const newlist = () => import('@/Views/homenav/NewsList.vue')
const pressrelease = () => import('@/Views/homenav/PressRelease.vue')
const reviewed = () => import('@/Views/homenav/Reviewed.vue')
const unapproved = () => import('@/Views/homenav/Unapproved.vue')
const Userinfo = () => import('@/Views/homenav/UserInfo.vue')
const auditlist = () => import('@/Views/homenav/AuditList.vue')
const UnewIndex = () => import('@/Views/homenav/UnewIndex.vue')
const newcontent = () => import('@/Views/homenav/NewContent.vue')
const newApprove = () => import('@/Views/homenav/NewsApprove.vue')
const newCollection = () => import('@/Views/homenav/NewsCollection.vue')


const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: login, name: 'login', title: '登录' },
    { path: '/register', component: register, name: 'register', title: '注册' },
    {
        path: '/Ahome', component: home, name: 'Ahome', title: '主页', redirect: '/Ahome/anewindex',
        children: [
            { name: 'anewindex', title: '管理员首页', path: 'anewindex', component: newindex, meta: { title: '管理员首页', path: '/Ahome/anewindex', name: 'anewindex' } },
            { name: 'auserinfo', title: '个人信息', path: 'auserinfo', component: Userinfo, meta: { title: '个人信息', path: '/Ahome/auserinfo', name: 'auserinfo' } },
            { name: 'reviewed', title: '已审核', path: 'reviewed', component: reviewed, meta: { title: '已审核', path: '/Ahome/reviewed', name: 'reviewed', } },
            { name: 'unapproved', title: '未审核', path: 'unapproved', component: unapproved, meta: { title: '未审核', path: '/Ahome/unapproved', name: 'unapproved', } },
            { name: 'newcolumn', title: '栏目管理', path: 'newcolumn', component: newcolumn, meta: { title: '栏目管理', path: '/Ahome/newcolumn', name: 'newcolumn', } },
            { name: 'newlist', title: '新闻列表', path: 'newlist', component: newlist, meta: { title: '新闻列表', path: '/Ahome/newlist', name: 'newlist' } },
        ]
    },
    {
        path: '/Uhome', component: home, name: 'Uhome', title: '主页', redirect: '/Uhome/unewindex',
        children: [
            { name: 'unewindex', title: '新闻首页', path: 'unewindex', component: UnewIndex, meta: { title: '新闻首页', path: '/Uhome/unewindex', name: 'unewindex', } },
            { name: 'auditlist', title: '审核列表', path: 'auditlist', component: auditlist, meta: { title: '审核列表', path: '/Uhome/auditlist', name: 'auditlist', } },
            { name: 'uuserinfo', title: '个人信息', path: 'uuserinfo', component: Userinfo, meta: { title: '个人信息', path: '/Uhome/uuserinfo', name: 'uuserinfo' } },
            { name: 'upressrelease', title: '新闻发布', path: 'upressrelease', component: pressrelease, meta: { title: '新闻发布', path: '/Uhome/upressrelease', name: 'upressrelease' } },
            { name: 'newsapprove', title: '新闻点赞', path: 'approve', component: newApprove, meta: { title: '新闻点赞', path: '/Uhome/newapprove', name: 'newapprove' } },
            { name: 'newscollection', title: '新闻收藏', path: 'collection', component: newCollection, meta: { title: '新闻收藏', path: '/Uhome/newcollection', name: 'newcollection' } }
        ]
    },
    {
        path: '/Uhome/unewindex/:newsId(\\d+)', component: newcontent, name: 'newcontent', props: true, title: '新闻详情页'
    },
    { path: '/:path(.*)', component: NotFound },//这里使用正则表达式，如果上面匹配不到就会匹配这里找不到页面的提示
]




const router = createRouter({
    history: createWebHashHistory(),
    routes,
    strict: true,//将匹配后缀不带反斜杠的url
    sensitive: true//将匹配不分大小写的url
})

router.beforeEach((to, from, next) => {
    start()
    const store = usestore()
    //导航栏路由数据
    const token = localStorage.getItem('token')
    if (to.name !== 'login' && !token && to.name !== 'register') next({ name: 'login' })
    next()
})

router.afterEach(() => {
    close()
})

export default router